home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_elisp-manual-19.idb / usr / freeware / info / elisp-29.z / elisp-29 (.txt)
GNU Info File  |  1998-05-26  |  47KB  |  863 lines

  1. This is Info file elisp, produced by Makeinfo-1.63 from the input file
  2. elisp.texi.
  3.    This version is the edition 2.4.2 of the GNU Emacs Lisp Reference
  4. Manual.  It corresponds to Emacs Version 19.34.
  5.    Published by the Free Software Foundation 59 Temple Place, Suite 330
  6. Boston, MA  02111-1307  USA
  7.    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996 Free Software
  8. Foundation, Inc.
  9.    Permission is granted to make and distribute verbatim copies of this
  10. manual provided the copyright notice and this permission notice are
  11. preserved on all copies.
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided that the
  14. entire resulting derived work is distributed under the terms of a
  15. permission notice identical to this one.
  16.    Permission is granted to copy and distribute translations of this
  17. manual into another language, under the above conditions for modified
  18. versions, except that this permission notice may be stated in a
  19. translation approved by the Foundation.
  20.    Permission is granted to copy and distribute modified versions of
  21. this manual under the conditions for verbatim copying, provided also
  22. that the section entitled "GNU General Public License" is included
  23. exactly as in the original, and provided that the entire resulting
  24. derived work is distributed under the terms of a permission notice
  25. identical to this one.
  26.    Permission is granted to copy and distribute translations of this
  27. manual into another language, under the above conditions for modified
  28. versions, except that the section entitled "GNU General Public License"
  29. may be included in a translation approved by the Free Software
  30. Foundation instead of in the original English.
  31. File: elisp,  Node: Regexp Search,  Next: POSIX Regexps,  Prev: Regular Expressions,  Up: Searching and Matching
  32. Regular Expression Searching
  33. ============================
  34.    In GNU Emacs, you can search for the next match for a regexp either
  35. incrementally or not.  For incremental search commands, see *Note
  36. Regular Expression Search: (emacs)Regexp Search.  Here we describe only
  37. the search functions useful in programs.  The principal one is
  38. `re-search-forward'.
  39.  - Command: re-search-forward REGEXP &optional LIMIT NOERROR REPEAT
  40.      This function searches forward in the current buffer for a string
  41.      of text that is matched by the regular expression REGEXP.  The
  42.      function skips over any amount of text that is not matched by
  43.      REGEXP, and leaves point at the end of the first match found.  It
  44.      returns the new value of point.
  45.      If LIMIT is non-`nil' (it must be a position in the current
  46.      buffer), then it is the upper bound to the search.  No match
  47.      extending after that position is accepted.
  48.      What happens when the search fails depends on the value of
  49.      NOERROR.  If NOERROR is `nil', a `search-failed' error is
  50.      signaled.  If NOERROR is `t', `re-search-forward' does nothing and
  51.      returns `nil'.  If NOERROR is neither `nil' nor `t', then
  52.      `re-search-forward' moves point to LIMIT (or the end of the
  53.      buffer) and returns `nil'.
  54.      If REPEAT is supplied (it must be a positive number), then the
  55.      search is repeated that many times (each time starting at the end
  56.      of the previous time's match).  If these successive searches
  57.      succeed, the function succeeds, moving point and returning its new
  58.      value.  Otherwise the search fails.
  59.      In the following example, point is initially before the `T'.
  60.      Evaluating the search call moves point to the end of that line
  61.      (between the `t' of `hat' and the newline).
  62.           ---------- Buffer: foo ----------
  63.           I read "-!-The cat in the hat
  64.           comes back" twice.
  65.           ---------- Buffer: foo ----------
  66.           
  67.           (re-search-forward "[a-z]+" nil t 5)
  68.                => 27
  69.           
  70.           ---------- Buffer: foo ----------
  71.           I read "The cat in the hat-!-
  72.           comes back" twice.
  73.           ---------- Buffer: foo ----------
  74.  - Command: re-search-backward REGEXP &optional LIMIT NOERROR REPEAT
  75.      This function searches backward in the current buffer for a string
  76.      of text that is matched by the regular expression REGEXP, leaving
  77.      point at the beginning of the first text found.
  78.      This function is analogous to `re-search-forward', but they are not
  79.      simple mirror images.  `re-search-forward' finds the match whose
  80.      beginning is as close as possible to the starting point.  If
  81.      `re-search-backward' were a perfect mirror image, it would find the
  82.      match whose end is as close as possible.  However, in fact it
  83.      finds the match whose beginning is as close as possible.  The
  84.      reason is that matching a regular expression at a given spot
  85.      always works from beginning to end, and starts at a specified
  86.      beginning position.
  87.      A true mirror-image of `re-search-forward' would require a special
  88.      feature for matching regexps from end to beginning.  It's not
  89.      worth the trouble of implementing that.
  90.  - Function: string-match REGEXP STRING &optional START
  91.      This function returns the index of the start of the first match for
  92.      the regular expression REGEXP in STRING, or `nil' if there is no
  93.      match.  If START is non-`nil', the search starts at that index in
  94.      STRING.
  95.      For example,
  96.           (string-match
  97.            "quick" "The quick brown fox jumped quickly.")
  98.                => 4
  99.           (string-match
  100.            "quick" "The quick brown fox jumped quickly." 8)
  101.                => 27
  102.      The index of the first character of the string is 0, the index of
  103.      the second character is 1, and so on.
  104.      After this function returns, the index of the first character
  105.      beyond the match is available as `(match-end 0)'.  *Note Match
  106.      Data::.
  107.           (string-match
  108.            "quick" "The quick brown fox jumped quickly." 8)
  109.                => 27
  110.           
  111.           (match-end 0)
  112.                => 32
  113.  - Function: looking-at REGEXP
  114.      This function determines whether the text in the current buffer
  115.      directly following point matches the regular expression REGEXP.
  116.      "Directly following" means precisely that: the search is
  117.      "anchored" and it can succeed only starting with the first
  118.      character following point.  The result is `t' if so, `nil'
  119.      otherwise.
  120.      This function does not move point, but it updates the match data,
  121.      which you can access using `match-beginning' and `match-end'.
  122.      *Note Match Data::.
  123.      In this example, point is located directly before the `T'.  If it
  124.      were anywhere else, the result would be `nil'.
  125.           ---------- Buffer: foo ----------
  126.           I read "-!-The cat in the hat
  127.           comes back" twice.
  128.           ---------- Buffer: foo ----------
  129.           
  130.           (looking-at "The cat in the hat$")
  131.                => t
  132. File: elisp,  Node: POSIX Regexps,  Next: Search and Replace,  Prev: Regexp Search,  Up: Searching and Matching
  133. POSIX Regular Expression Searching
  134. ==================================
  135.    The usual regular expression functions do backtracking when necessary
  136. to handle the `\|' and repetition constructs, but they continue this
  137. only until they find *some* match.  Then they succeed and report the
  138. first match found.
  139.    This section describes alternative search functions which perform the
  140. full backtracking specified by the POSIX standard for regular expression
  141. matching.  They continue backtracking until they have tried all
  142. possibilities and found all matches, so they can report the longest
  143. match, as required by POSIX.  This is much slower, so use these
  144. functions only when you really need the longest match.
  145.    In Emacs versions prior to 19.29, these functions did not exist, and
  146. the functions described above implemented full POSIX backtracking.
  147.  - Function: posix-search-forward REGEXP &optional LIMIT NOERROR REPEAT
  148.      This is like `re-search-forward' except that it performs the full
  149.      backtracking specified by the POSIX standard for regular expression
  150.      matching.
  151.  - Function: posix-search-backward REGEXP &optional LIMIT NOERROR REPEAT
  152.      This is like `re-search-backward' except that it performs the full
  153.      backtracking specified by the POSIX standard for regular expression
  154.      matching.
  155.  - Function: posix-looking-at REGEXP
  156.      This is like `looking-at' except that it performs the full
  157.      backtracking specified by the POSIX standard for regular expression
  158.      matching.
  159.  - Function: posix-string-match REGEXP STRING &optional START
  160.      This is like `string-match' except that it performs the full
  161.      backtracking specified by the POSIX standard for regular expression
  162.      matching.
  163. File: elisp,  Node: Search and Replace,  Next: Match Data,  Prev: POSIX Regexps,  Up: Searching and Matching
  164. Search and Replace
  165. ==================
  166.  - Function: perform-replace FROM-STRING REPLACEMENTS QUERY-FLAG
  167.           REGEXP-FLAG DELIMITED-FLAG &optional REPEAT-COUNT MAP
  168.      This function is the guts of `query-replace' and related commands.
  169.      It searches for occurrences of FROM-STRING and replaces some or
  170.      all of them.  If QUERY-FLAG is `nil', it replaces all occurrences;
  171.      otherwise, it asks the user what to do about each one.
  172.      If REGEXP-FLAG is non-`nil', then FROM-STRING is considered a
  173.      regular expression; otherwise, it must match literally.  If
  174.      DELIMITED-FLAG is non-`nil', then only replacements surrounded by
  175.      word boundaries are considered.
  176.      The argument REPLACEMENTS specifies what to replace occurrences
  177.      with.  If it is a string, that string is used.  It can also be a
  178.      list of strings, to be used in cyclic order.
  179.      If REPEAT-COUNT is non-`nil', it should be an integer.  Then it
  180.      specifies how many times to use each of the strings in the
  181.      REPLACEMENTS list before advancing cyclicly to the next one.
  182.      Normally, the keymap `query-replace-map' defines the possible user
  183.      responses for queries.  The argument MAP, if non-`nil', is a
  184.      keymap to use instead of `query-replace-map'.
  185.  - Variable: query-replace-map
  186.      This variable holds a special keymap that defines the valid user
  187.      responses for `query-replace' and related functions, as well as
  188.      `y-or-n-p' and `map-y-or-n-p'.  It is unusual in two ways:
  189.         * The "key bindings" are not commands, just symbols that are
  190.           meaningful to the functions that use this map.
  191.         * Prefix keys are not supported; each key binding must be for a
  192.           single event key sequence.  This is because the functions
  193.           don't use read key sequence to get the input; instead, they
  194.           read a single event and look it up "by hand."
  195.    Here are the meaningful "bindings" for `query-replace-map'.  Several
  196. of them are meaningful only for `query-replace' and friends.
  197. `act'
  198.      Do take the action being considered--in other words, "yes."
  199. `skip'
  200.      Do not take action for this question--in other words, "no."
  201. `exit'
  202.      Answer this question "no," and give up on the entire series of
  203.      questions, assuming that the answers will be "no."
  204. `act-and-exit'
  205.      Answer this question "yes," and give up on the entire series of
  206.      questions, assuming that subsequent answers will be "no."
  207. `act-and-show'
  208.      Answer this question "yes," but show the results--don't advance yet
  209.      to the next question.
  210. `automatic'
  211.      Answer this question and all subsequent questions in the series
  212.      with "yes," without further user interaction.
  213. `backup'
  214.      Move back to the previous place that a question was asked about.
  215. `edit'
  216.      Enter a recursive edit to deal with this question--instead of any
  217.      other action that would normally be taken.
  218. `delete-and-edit'
  219.      Delete the text being considered, then enter a recursive edit to
  220.      replace it.
  221. `recenter'
  222.      Redisplay and center the window, then ask the same question again.
  223. `quit'
  224.      Perform a quit right away.  Only `y-or-n-p' and related functions
  225.      use this answer.
  226. `help'
  227.      Display some help, then ask again.
  228. File: elisp,  Node: Match Data,  Next: Searching and Case,  Prev: Search and Replace,  Up: Searching and Matching
  229. The Match Data
  230. ==============
  231.    Emacs keeps track of the positions of the start and end of segments
  232. of text found during a regular expression search.  This means, for
  233. example, that you can search for a complex pattern, such as a date in
  234. an Rmail message, and then extract parts of the match under control of
  235. the pattern.
  236.    Because the match data normally describe the most recent search only,
  237. you must be careful not to do another search inadvertently between the
  238. search you wish to refer back to and the use of the match data.  If you
  239. can't avoid another intervening search, you must save and restore the
  240. match data around it, to prevent it from being overwritten.
  241. * Menu:
  242. * Simple Match Data::     Accessing single items of match data,
  243.                 such as where a particular subexpression started.
  244. * Replacing Match::      Replacing a substring that was matched.
  245. * Entire Match Data::     Accessing the entire match data at once, as a list.
  246. * Saving Match Data::     Saving and restoring the match data.
  247. File: elisp,  Node: Simple Match Data,  Next: Replacing Match,  Up: Match Data
  248. Simple Match Data Access
  249. ------------------------
  250.    This section explains how to use the match data to find out what was
  251. matched by the last search or match operation.
  252.    You can ask about the entire matching text, or about a particular
  253. parenthetical subexpression of a regular expression.  The COUNT
  254. argument in the functions below specifies which.  If COUNT is zero, you
  255. are asking about the entire match.  If COUNT is positive, it specifies
  256. which subexpression you want.
  257.    Recall that the subexpressions of a regular expression are those
  258. expressions grouped with escaped parentheses, `\(...\)'.  The COUNTth
  259. subexpression is found by counting occurrences of `\(' from the
  260. beginning of the whole regular expression.  The first subexpression is
  261. numbered 1, the second 2, and so on.  Only regular expressions can have
  262. subexpressions--after a simple string search, the only information
  263. available is about the entire match.
  264.  - Function: match-string COUNT &optional IN-STRING
  265.      This function returns, as a string, the text matched in the last
  266.      search or match operation.  It returns the entire text if COUNT is
  267.      zero, or just the portion corresponding to the COUNTth
  268.      parenthetical subexpression, if COUNT is positive.  If COUNT is
  269.      out of range, or if that subexpression didn't match anything, the
  270.      value is `nil'.
  271.      If the last such operation was done against a string with
  272.      `string-match', then you should pass the same string as the
  273.      argument IN-STRING.  Otherwise, after a buffer search or match,
  274.      you should omit IN-STRING or pass `nil' for it; but you should
  275.      make sure that the current buffer when you call `match-string' is
  276.      the one in which you did the searching or matching.
  277.  - Function: match-beginning COUNT
  278.      This function returns the position of the start of text matched by
  279.      the last regular expression searched for, or a subexpression of it.
  280.      If COUNT is zero, then the value is the position of the start of
  281.      the entire match.  Otherwise, COUNT specifies a subexpression in
  282.      the regular expresion, and the value of the function is the
  283.      starting position of the match for that subexpression.
  284.      The value is `nil' for a subexpression inside a `\|' alternative
  285.      that wasn't used in the match.
  286.  - Function: match-end COUNT
  287.      This function is like `match-beginning' except that it returns the
  288.      position of the end of the match, rather than the position of the
  289.      beginning.
  290.    Here is an example of using the match data, with a comment showing
  291. the positions within the text:
  292.      (string-match "\\(qu\\)\\(ick\\)"
  293.                    "The quick fox jumped quickly.")
  294.                    ;0123456789
  295.           => 4
  296.      
  297.      (match-string 0 "The quick fox jumped quickly.")
  298.           => "quick"
  299.      (match-string 1 "The quick fox jumped quickly.")
  300.           => "qu"
  301.      (match-string 2 "The quick fox jumped quickly.")
  302.           => "ick"
  303.      
  304.      (match-beginning 1)       ; The beginning of the match
  305.           => 4                 ;   with `qu' is at index 4.
  306.      
  307.      (match-beginning 2)       ; The beginning of the match
  308.           => 6                 ;   with `ick' is at index 6.
  309.      
  310.      (match-end 1)             ; The end of the match
  311.           => 6                 ;   with `qu' is at index 6.
  312.      
  313.      (match-end 2)             ; The end of the match
  314.           => 9                 ;   with `ick' is at index 9.
  315.    Here is another example.  Point is initially located at the beginning
  316. of the line.  Searching moves point to between the space and the word
  317. `in'.  The beginning of the entire match is at the 9th character of the
  318. buffer (`T'), and the beginning of the match for the first
  319. subexpression is at the 13th character (`c').
  320.      (list
  321.        (re-search-forward "The \\(cat \\)")
  322.        (match-beginning 0)
  323.        (match-beginning 1))
  324.          => (9 9 13)
  325.      
  326.      ---------- Buffer: foo ----------
  327.      I read "The cat -!-in the hat comes back" twice.
  328.              ^   ^
  329.              9  13
  330.      ---------- Buffer: foo ----------
  331. (In this case, the index returned is a buffer position; the first
  332. character of the buffer counts as 1.)
  333. File: elisp,  Node: Replacing Match,  Next: Entire Match Data,  Prev: Simple Match Data,  Up: Match Data
  334. Replacing the Text That Matched
  335. -------------------------------
  336.    This function replaces the text matched by the last search with
  337. REPLACEMENT.
  338.  - Function: replace-match REPLACEMENT &optional FIXEDCASE LITERAL
  339.           STRING SUBEXP
  340.      This function replaces the text in the buffer (or in STRING) that
  341.      was matched by the last search.  It replaces that text with
  342.      REPLACEMENT.
  343.      If you did the last search in a buffer, you should specify `nil'
  344.      for STRING.  Then `replace-match' does the replacement by editing
  345.      the buffer; it leaves point at the end of the replacement text,
  346.      and returns `t'.
  347.      If you did the search in a string, pass the same string as STRING.
  348.      Then `replace-match' does the replacement by constructing and
  349.      returning a new string.
  350.      If FIXEDCASE is non-`nil', then the case of the replacement text
  351.      is not changed; otherwise, the replacement text is converted to a
  352.      different case depending upon the capitalization of the text to be
  353.      replaced.  If the original text is all upper case, the replacement
  354.      text is converted to upper case.  If the first word of the
  355.      original text is capitalized, then the first word of the
  356.      replacement text is capitalized.  If the original text contains
  357.      just one word, and that word is a capital letter, `replace-match'
  358.      considers this a capitalized first word rather than all upper case.
  359.      If `case-replace' is `nil', then case conversion is not done,
  360.      regardless of the value of FIXED-CASE.  *Note Searching and Case::.
  361.      If LITERAL is non-`nil', then REPLACEMENT is inserted exactly as
  362.      it is, the only alterations being case changes as needed.  If it
  363.      is `nil' (the default), then the character `\' is treated
  364.      specially.  If a `\' appears in REPLACEMENT, then it must be part
  365.      of one of the following sequences:
  366.     `\&'
  367.           `\&' stands for the entire text being replaced.
  368.     `\N'
  369.           `\N', where N is a digit, stands for the text that matched
  370.           the Nth subexpression in the original regexp.  Subexpressions
  371.           are those expressions grouped inside `\(...\)'.
  372.     `\\'
  373.           `\\' stands for a single `\' in the replacement text.
  374.      If SUBEXP is non-`nil', that says to replace just subexpression
  375.      number SUBEXP of the regexp that was matched, not the entire
  376.      match.  For example, after matching `foo \(ba*r\)', calling
  377.      `replace-match' with 1 as SUBEXP means to replace just the text
  378.      that matched `\(ba*r\)'.
  379. File: elisp,  Node: Entire Match Data,  Next: Saving Match Data,  Prev: Replacing Match,  Up: Match Data
  380. Accessing the Entire Match Data
  381. -------------------------------
  382.    The functions `match-data' and `set-match-data' read or write the
  383. entire match data, all at once.
  384.  - Function: match-data
  385.      This function returns a newly constructed list containing all the
  386.      information on what text the last search matched.  Element zero is
  387.      the position of the beginning of the match for the whole
  388.      expression; element one is the position of the end of the match
  389.      for the expression.  The next two elements are the positions of
  390.      the beginning and end of the match for the first subexpression,
  391.      and so on.  In general, element number 2N corresponds to
  392.      `(match-beginning N)'; and element number 2N + 1 corresponds to
  393.      `(match-end N)'.
  394.      All the elements are markers or `nil' if matching was done on a
  395.      buffer, and all are integers or `nil' if matching was done on a
  396.      string with `string-match'.  (In Emacs 18 and earlier versions,
  397.      markers were used even for matching on a string, except in the case
  398.      of the integer 0.)
  399.      As always, there must be no possibility of intervening searches
  400.      between the call to a search function and the call to `match-data'
  401.      that is intended to access the match data for that search.
  402.           (match-data)
  403.                =>  (#<marker at 9 in foo>
  404.                     #<marker at 17 in foo>
  405.                     #<marker at 13 in foo>
  406.                     #<marker at 17 in foo>)
  407.  - Function: set-match-data MATCH-LIST
  408.      This function sets the match data from the elements of MATCH-LIST,
  409.      which should be a list that was the value of a previous call to
  410.      `match-data'.
  411.      If MATCH-LIST refers to a buffer that doesn't exist, you don't get
  412.      an error; that sets the match data in a meaningless but harmless
  413.      way.
  414.      `store-match-data' is an alias for `set-match-data'.
  415. File: elisp,  Node: Saving Match Data,  Prev: Entire Match Data,  Up: Match Data
  416. Saving and Restoring the Match Data
  417. -----------------------------------
  418.    When you call a function that may do a search, you may need to save
  419. and restore the match data around that call, if you want to preserve the
  420. match data from an earlier search for later use.  Here is an example
  421. that shows the problem that arises if you fail to save the match data:
  422.      (re-search-forward "The \\(cat \\)")
  423.           => 48
  424.      (foo)                   ; Perhaps `foo' does
  425.                              ;   more searching.
  426.      (match-end 0)
  427.           => 61              ; Unexpected result---not 48!
  428.    You can save and restore the match data with `save-match-data':
  429.  - Macro: save-match-data BODY...
  430.      This special form executes BODY, saving and restoring the match
  431.      data around it.
  432.    You can use `set-match-data' together with `match-data' to imitate
  433. the effect of the special form `save-match-data'.  This is useful for
  434. writing code that can run in Emacs 18.  Here is how:
  435.      (let ((data (match-data)))
  436.        (unwind-protect
  437.            ...   ; May change the original match data.
  438.          (set-match-data data)))
  439.    Emacs automatically saves and restores the match data when it runs
  440. process filter functions (*note Filter Functions::.) and process
  441. sentinels (*note Sentinels::.).
  442. File: elisp,  Node: Searching and Case,  Next: Standard Regexps,  Prev: Match Data,  Up: Searching and Matching
  443. Searching and Case
  444. ==================
  445.    By default, searches in Emacs ignore the case of the text they are
  446. searching through; if you specify searching for `FOO', then `Foo' or
  447. `foo' is also considered a match.  Regexps, and in particular character
  448. sets, are included: thus, `[aB]' would match `a' or `A' or `b' or `B'.
  449.    If you do not want this feature, set the variable `case-fold-search'
  450. to `nil'.  Then all letters must match exactly, including case.  This
  451. is a buffer-local variable; altering the variable affects only the
  452. current buffer.  (*Note Intro to Buffer-Local::.)  Alternatively, you
  453. may change the value of `default-case-fold-search', which is the
  454. default value of `case-fold-search' for buffers that do not override it.
  455.    Note that the user-level incremental search feature handles case
  456. distinctions differently.  When given a lower case letter, it looks for
  457. a match of either case, but when given an upper case letter, it looks
  458. for an upper case letter only.  But this has nothing to do with the
  459. searching functions Lisp functions use.
  460.  - User Option: case-replace
  461.      This variable determines whether the replacement functions should
  462.      preserve case.  If the variable is `nil', that means to use the
  463.      replacement text verbatim.  A non-`nil' value means to convert the
  464.      case of the replacement text according to the text being replaced.
  465.      The function `replace-match' is where this variable actually has
  466.      its effect.  *Note Replacing Match::.
  467.  - User Option: case-fold-search
  468.      This buffer-local variable determines whether searches should
  469.      ignore case.  If the variable is `nil' they do not ignore case;
  470.      otherwise they do ignore case.
  471.  - Variable: default-case-fold-search
  472.      The value of this variable is the default value for
  473.      `case-fold-search' in buffers that do not override it.  This is the
  474.      same as `(default-value 'case-fold-search)'.
  475. File: elisp,  Node: Standard Regexps,  Prev: Searching and Case,  Up: Searching and Matching
  476. Standard Regular Expressions Used in Editing
  477. ============================================
  478.    This section describes some variables that hold regular expressions
  479. used for certain purposes in editing:
  480.  - Variable: page-delimiter
  481.      This is the regexp describing line-beginnings that separate pages.
  482.      The default value is `"^\014"' (i.e., `"^^L"' or `"^\C-l"'); this
  483.      matches a line that starts with a formfeed character.
  484.    The following two regular expressions should *not* assume the match
  485. always starts at the beginning of a line; they should not use `^' to
  486. anchor the match.  Most often, the paragraph commands do check for a
  487. match only at the beginning of a line, which means that `^' would be
  488. superfluous.  When there is a nonzero left margin, they accept matches
  489. that start after the left margin.  In that case, a `^' would be
  490. incorrect.  However, a `^' is harmless in modes where a left margin is
  491. never used.
  492.  - Variable: paragraph-separate
  493.      This is the regular expression for recognizing the beginning of a
  494.      line that separates paragraphs.  (If you change this, you may have
  495.      to change `paragraph-start' also.)  The default value is
  496.      `"[ \t\f]*$"', which matches a line that consists entirely of
  497.      spaces, tabs, and form feeds (after its left margin).
  498.  - Variable: paragraph-start
  499.      This is the regular expression for recognizing the beginning of a
  500.      line that starts *or* separates paragraphs.  The default value is
  501.      `"[ \t\n\f]"', which matches a line starting with a space, tab,
  502.      newline, or form feed (after its left margin).
  503.  - Variable: sentence-end
  504.      This is the regular expression describing the end of a sentence.
  505.      (All paragraph boundaries also end sentences, regardless.)  The
  506.      default value is:
  507.           "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*"
  508.      This means a period, question mark or exclamation mark, followed
  509.      optionally by a closing parenthetical character, followed by tabs,
  510.      spaces or new lines.
  511.      For a detailed explanation of this regular expression, see *Note
  512.      Regexp Example::.
  513. File: elisp,  Node: Syntax Tables,  Next: Abbrevs,  Prev: Searching and Matching,  Up: Top
  514. Syntax Tables
  515. *************
  516.    A "syntax table" specifies the syntactic textual function of each
  517. character.  This information is used by the parsing commands, the
  518. complex movement commands, and others to determine where words, symbols,
  519. and other syntactic constructs begin and end.  The current syntax table
  520. controls the meaning of the word motion functions (*note Word Motion::.)
  521. and the list motion functions (*note List Motion::.) as well as the
  522. functions in this chapter.
  523. * Menu:
  524. * Basics: Syntax Basics.     Basic concepts of syntax tables.
  525. * Desc: Syntax Descriptors.  How characters are classified.
  526. * Syntax Table Functions::   How to create, examine and alter syntax tables.
  527. * Motion and Syntax::         Moving over characters with certain syntaxes.
  528. * Parsing Expressions::      Parsing balanced expressions
  529.                                 using the syntax table.
  530. * Standard Syntax Tables::   Syntax tables used by various major modes.
  531. * Syntax Table Internals::   How syntax table information is stored.
  532. File: elisp,  Node: Syntax Basics,  Next: Syntax Descriptors,  Up: Syntax Tables
  533. Syntax Table Concepts
  534. =====================
  535.    A "syntax table" provides Emacs with the information that determines
  536. the syntactic use of each character in a buffer.  This information is
  537. used by the parsing commands, the complex movement commands, and others
  538. to determine where words, symbols, and other syntactic constructs begin
  539. and end.  The current syntax table controls the meaning of the word
  540. motion functions (*note Word Motion::.) and the list motion functions
  541. (*note List Motion::.) as well as the functions in this chapter.
  542.    A syntax table is a vector of 256 elements; it contains one entry for
  543. each of the 256 possible characters in an 8-bit byte.  Each element is
  544. an integer that encodes the syntax of the character in question.
  545.    Syntax tables are used only for moving across text, not for the Emacs
  546. Lisp reader.  Emacs Lisp uses built-in syntactic rules when reading Lisp
  547. expressions, and these rules cannot be changed.
  548.    Each buffer has its own major mode, and each major mode has its own
  549. idea of the syntactic class of various characters.  For example, in Lisp
  550. mode, the character `;' begins a comment, but in C mode, it terminates
  551. a statement.  To support these variations, Emacs makes the choice of
  552. syntax table local to each buffer.  Typically, each major mode has its
  553. own syntax table and installs that table in each buffer that uses that
  554. mode.  Changing this table alters the syntax in all those buffers as
  555. well as in any buffers subsequently put in that mode.  Occasionally
  556. several similar modes share one syntax table.  *Note Example Major
  557. Modes::, for an example of how to set up a syntax table.
  558.    A syntax table can inherit the data for some characters from the
  559. standard syntax table, while specifying other characters itself.  The
  560. "inherit" syntax class means "inherit this character's syntax from the
  561. standard syntax table."  Most major modes' syntax tables inherit the
  562. syntax of character codes 0 through 31 and 128 through 255.  This is
  563. useful with character sets such as ISO Latin-1 that have additional
  564. alphabetic characters in the range 128 to 255.  Just changing the
  565. standard syntax for these characters affects all major modes.
  566.  - Function: syntax-table-p OBJECT
  567.      This function returns `t' if OBJECT is a vector of length 256
  568.      elements.  This means that the vector may be a syntax table.
  569.      However, according to this test, any vector of length 256 is
  570.      considered to be a syntax table, no matter what its contents.
  571. File: elisp,  Node: Syntax Descriptors,  Next: Syntax Table Functions,  Prev: Syntax Basics,  Up: Syntax Tables
  572. Syntax Descriptors
  573. ==================
  574.    This section describes the syntax classes and flags that denote the
  575. syntax of a character, and how they are represented as a "syntax
  576. descriptor", which is a Lisp string that you pass to
  577. `modify-syntax-entry' to specify the desired syntax.
  578.    Emacs defines a number of "syntax classes".  Each syntax table puts
  579. each character into one class.  There is no necessary relationship
  580. between the class of a character in one syntax table and its class in
  581. any other table.
  582.    Each class is designated by a mnemonic character, which serves as the
  583. name of the class when you need to specify a class.  Usually the
  584. designator character is one that is frequently in that class; however,
  585. its meaning as a designator is unvarying and independent of what syntax
  586. that character currently has.
  587.    A syntax descriptor is a Lisp string that specifies a syntax class, a
  588. matching character (used only for the parenthesis classes) and flags.
  589. The first character is the designator for a syntax class.  The second
  590. character is the character to match; if it is unused, put a space there.
  591. Then come the characters for any desired flags.  If no matching
  592. character or flags are needed, one character is sufficient.
  593.    For example, the descriptor for the character `*' in C mode is
  594. `. 23' (i.e., punctuation, matching character slot unused, second
  595. character of a comment-starter, first character of an comment-ender),
  596. and the entry for `/' is `. 14' (i.e., punctuation, matching character
  597. slot unused, first character of a comment-starter, second character of
  598. a comment-ender).
  599. * Menu:
  600. * Syntax Class Table::      Table of syntax classes.
  601. * Syntax Flags::            Additional flags each character can have.
  602. File: elisp,  Node: Syntax Class Table,  Next: Syntax Flags,  Up: Syntax Descriptors
  603. Table of Syntax Classes
  604. -----------------------
  605.    Here is a table of syntax classes, the characters that stand for
  606. them, their meanings, and examples of their use.
  607.  - Syntax class: whitespace character
  608.      "Whitespace characters" (designated with ` ' or `-') separate
  609.      symbols and words from each other.  Typically, whitespace
  610.      characters have no other syntactic significance, and multiple
  611.      whitespace characters are syntactically equivalent to a single
  612.      one.  Space, tab, newline and formfeed are almost always
  613.      classified as whitespace.
  614.  - Syntax class: word constituent
  615.      "Word constituents" (designated with `w') are parts of normal
  616.      English words and are typically used in variable and command names
  617.      in programs.  All upper- and lower-case letters, and the digits,
  618.      are typically word constituents.
  619.  - Syntax class: symbol constituent
  620.      "Symbol constituents" (designated with `_') are the extra
  621.      characters that are used in variable and command names along with
  622.      word constituents.  For example, the symbol constituents class is
  623.      used in Lisp mode to indicate that certain characters may be part
  624.      of symbol names even though they are not part of English words.
  625.      These characters are `$&*+-_<>'.  In standard C, the only
  626.      non-word-constituent character that is valid in symbols is
  627.      underscore (`_').
  628.  - Syntax class: punctuation character
  629.      "Punctuation characters" (`.') are those characters that are used
  630.      as punctuation in English, or are used in some way in a programming
  631.      language to separate symbols from one another.  Most programming
  632.      language modes, including Emacs Lisp mode, have no characters in
  633.      this class since the few characters that are not symbol or word
  634.      constituents all have other uses.
  635.  - Syntax class: open parenthesis character
  636.  - Syntax class: close parenthesis character
  637.      Open and close "parenthesis characters" are characters used in
  638.      dissimilar pairs to surround sentences or expressions.  Such a
  639.      grouping is begun with an open parenthesis character and
  640.      terminated with a close.  Each open parenthesis character matches
  641.      a particular close parenthesis character, and vice versa.
  642.      Normally, Emacs indicates momentarily the matching open
  643.      parenthesis when you insert a close parenthesis.  *Note Blinking::.
  644.      The class of open parentheses is designated with `(', and that of
  645.      close parentheses with `)'.
  646.      In English text, and in C code, the parenthesis pairs are `()',
  647.      `[]', and `{}'.  In Emacs Lisp, the delimiters for lists and
  648.      vectors (`()' and `[]') are classified as parenthesis characters.
  649.  - Syntax class: string quote
  650.      "String quote characters" (designated with `"') are used in many
  651.      languages, including Lisp and C, to delimit string constants.  The
  652.      same string quote character appears at the beginning and the end
  653.      of a string.  Such quoted strings do not nest.
  654.      The parsing facilities of Emacs consider a string as a single
  655.      token.  The usual syntactic meanings of the characters in the
  656.      string are suppressed.
  657.      The Lisp modes have two string quote characters: double-quote (`"')
  658.      and vertical bar (`|').  `|' is not used in Emacs Lisp, but it is
  659.      used in Common Lisp.  C also has two string quote characters:
  660.      double-quote for strings, and single-quote (`'') for character
  661.      constants.
  662.      English text has no string quote characters because English is not
  663.      a programming language.  Although quotation marks are used in
  664.      English, we do not want them to turn off the usual syntactic
  665.      properties of other characters in the quotation.
  666.  - Syntax class: escape
  667.      An "escape character" (designated with `\') starts an escape
  668.      sequence such as is used in C string and character constants.  The
  669.      character `\' belongs to this class in both C and Lisp.  (In C, it
  670.      is used thus only inside strings, but it turns out to cause no
  671.      trouble to treat it this way throughout C code.)
  672.      Characters in this class count as part of words if
  673.      `words-include-escapes' is non-`nil'.  *Note Word Motion::.
  674.  - Syntax class: character quote
  675.      A "character quote character" (designated with `/') quotes the
  676.      following character so that it loses its normal syntactic meaning.
  677.      This differs from an escape character in that only the character
  678.      immediately following is ever affected.
  679.      Characters in this class count as part of words if
  680.      `words-include-escapes' is non-`nil'.  *Note Word Motion::.
  681.      This class is used for backslash in TeX mode.
  682.  - Syntax class: paired delimiter
  683.      "Paired delimiter characters" (designated with `$') are like
  684.      string quote characters except that the syntactic properties of the
  685.      characters between the delimiters are not suppressed.  Only TeX
  686.      mode uses a paired delimiter presently--the `$' that both enters
  687.      and leaves math mode.
  688.  - Syntax class: expression prefix
  689.      An "expression prefix operator" (designated with `'') is used for
  690.      syntactic operators that are part of an expression if they appear
  691.      next to one.  These characters in Lisp include the apostrophe, `''
  692.      (used for quoting), the comma, `,' (used in macros), and `#' (used
  693.      in the read syntax for certain data types).
  694.  - Syntax class: comment starter
  695.  - Syntax class: comment ender
  696.      The "comment starter" and "comment ender" characters are used in
  697.      various languages to delimit comments.  These classes are
  698.      designated with `<' and `>', respectively.
  699.      English text has no comment characters.  In Lisp, the semicolon
  700.      (`;') starts a comment and a newline or formfeed ends one.
  701.  - Syntax class: inherit
  702.      This syntax class does not specify a syntax.  It says to look in
  703.      the standard syntax table to find the syntax of this character.
  704.      The designator for this syntax code is `@'.
  705. File: elisp,  Node: Syntax Flags,  Prev: Syntax Class Table,  Up: Syntax Descriptors
  706. Syntax Flags
  707. ------------
  708.    In addition to the classes, entries for characters in a syntax table
  709. can include flags.  There are six possible flags, represented by the
  710. characters `1', `2', `3', `4', `b' and `p'.
  711.    All the flags except `p' are used to describe multi-character
  712. comment delimiters.  The digit flags indicate that a character can
  713. *also* be part of a comment sequence, in addition to the syntactic
  714. properties associated with its character class.  The flags are
  715. independent of the class and each other for the sake of characters such
  716. as `*' in C mode, which is a punctuation character, *and* the second
  717. character of a start-of-comment sequence (`/*'), *and* the first
  718. character of an end-of-comment sequence (`*/').
  719.    The flags for a character C are:
  720.    * `1' means C is the start of a two-character comment-start sequence.
  721.    * `2' means C is the second character of such a sequence.
  722.    * `3' means C is the start of a two-character comment-end sequence.
  723.    * `4' means C is the second character of such a sequence.
  724.    * `b' means that C as a comment delimiter belongs to the alternative
  725.      "b" comment style.
  726.      Emacs supports two comment styles simultaneously in any one syntax
  727.      table.  This is for the sake of C++.  Each style of comment syntax
  728.      has its own comment-start sequence and its own comment-end
  729.      sequence.  Each comment must stick to one style or the other;
  730.      thus, if it starts with the comment-start sequence of style "b",
  731.      it must also end with the comment-end sequence of style "b".
  732.      The two comment-start sequences must begin with the same
  733.      character; only the second character may differ.  Mark the second
  734.      character of the "b"-style comment-start sequence with the `b'
  735.      flag.
  736.      A comment-end sequence (one or two characters) applies to the "b"
  737.      style if its first character has the `b' flag set; otherwise, it
  738.      applies to the "a" style.
  739.      The appropriate comment syntax settings for C++ are as follows:
  740.     `/'
  741.           `124b'
  742.     `*'
  743.           `23'
  744.     newline
  745.           `>b'
  746.      This defines four comment-delimiting sequences:
  747.     `/*'
  748.           This is a comment-start sequence for "a" style because the
  749.           second character, `*', does not have the `b' flag.
  750.     `//'
  751.           This is a comment-start sequence for "b" style because the
  752.           second character, `/', does have the `b' flag.
  753.     `*/'
  754.           This is a comment-end sequence for "a" style because the first
  755.           character, `*', does not have the `b' flag
  756.     newline
  757.           This is a comment-end sequence for "b" style, because the
  758.           newline character has the `b' flag.
  759.    * `p' identifies an additional "prefix character" for Lisp syntax.
  760.      These characters are treated as whitespace when they appear between
  761.      expressions.  When they appear within an expression, they are
  762.      handled according to their usual syntax codes.
  763.      The function `backward-prefix-chars' moves back over these
  764.      characters, as well as over characters whose primary syntax class
  765.      is prefix (`'').  *Note Motion and Syntax::.
  766. File: elisp,  Node: Syntax Table Functions,  Next: Motion and Syntax,  Prev: Syntax Descriptors,  Up: Syntax Tables
  767. Syntax Table Functions
  768. ======================
  769.    In this section we describe functions for creating, accessing and
  770. altering syntax tables.
  771.  - Function: make-syntax-table
  772.      This function creates a new syntax table.  Character codes 0
  773.      through 31 and 128 through 255 are set up to inherit from the
  774.      standard syntax table.  The other character codes are set up by
  775.      copying what the standard syntax table says about them.
  776.      Most major mode syntax tables are created in this way.
  777.  - Function: copy-syntax-table &optional TABLE
  778.      This function constructs a copy of TABLE and returns it.  If TABLE
  779.      is not supplied (or is `nil'), it returns a copy of the current
  780.      syntax table.  Otherwise, an error is signaled if TABLE is not a
  781.      syntax table.
  782.  - Command: modify-syntax-entry CHAR SYNTAX-DESCRIPTOR &optional TABLE
  783.      This function sets the syntax entry for CHAR according to
  784.      SYNTAX-DESCRIPTOR.  The syntax is changed only for TABLE, which
  785.      defaults to the current buffer's syntax table, and not in any
  786.      other syntax table.  The argument SYNTAX-DESCRIPTOR specifies the
  787.      desired syntax; this is a string beginning with a class designator
  788.      character, and optionally containing a matching character and
  789.      flags as well.  *Note Syntax Descriptors::.
  790.      This function always returns `nil'.  The old syntax information in
  791.      the table for this character is discarded.
  792.      An error is signaled if the first character of the syntax
  793.      descriptor is not one of the twelve syntax class designator
  794.      characters.  An error is also signaled if CHAR is not a character.
  795.      Examples:
  796.           ;; Put the space character in class whitespace.
  797.           (modify-syntax-entry ?\  " ")
  798.                => nil
  799.           
  800.           ;; Make `$' an open parenthesis character,
  801.           ;;   with `^' as its matching close.
  802.           (modify-syntax-entry ?$ "(^")
  803.                => nil
  804.           
  805.           ;; Make `^' a close parenthesis character,
  806.           ;;   with `$' as its matching open.
  807.           (modify-syntax-entry ?^ ")$")
  808.                => nil
  809.           
  810.           ;; Make `/' a punctuation character,
  811.           ;;   the first character of a start-comment sequence,
  812.           ;;   and the second character of an end-comment sequence.
  813.           ;;   This is used in C mode.
  814.           (modify-syntax-entry ?/ ". 14")
  815.                => nil
  816.  - Function: char-syntax CHARACTER
  817.      This function returns the syntax class of CHARACTER, represented
  818.      by its mnemonic designator character.  This *only* returns the
  819.      class, not any matching parenthesis or flags.
  820.      An error is signaled if CHAR is not a character.
  821.      The following examples apply to C mode.  The first example shows
  822.      that the syntax class of space is whitespace (represented by a
  823.      space).  The second example shows that the syntax of `/' is
  824.      punctuation.  This does not show the fact that it is also part of
  825.      comment-start and -end sequences.  The third example shows that
  826.      open parenthesis is in the class of open parentheses.  This does
  827.      not show the fact that it has a matching character, `)'.
  828.           (char-to-string (char-syntax ?\ ))
  829.                => " "
  830.           
  831.           (char-to-string (char-syntax ?/))
  832.                => "."
  833.           
  834.           (char-to-string (char-syntax ?\())
  835.                => "("
  836.  - Function: set-syntax-table TABLE
  837.      This function makes TABLE the syntax table for the current buffer.
  838.      It returns TABLE.
  839.  - Function: syntax-table
  840.      This function returns the current syntax table, which is the table
  841.      for the current buffer.
  842. File: elisp,  Node: Motion and Syntax,  Next: Parsing Expressions,  Prev: Syntax Table Functions,  Up: Syntax Tables
  843. Motion and Syntax
  844. =================
  845.    This section describes functions for moving across characters in
  846. certain syntax classes.  None of these functions exists in Emacs
  847. version 18 or earlier.
  848.  - Function: skip-syntax-forward SYNTAXES &optional LIMIT
  849.      This function moves point forward across characters having syntax
  850.      classes mentioned in SYNTAXES.  It stops when it encounters the
  851.      end of the buffer, or position LIMIT (if specified), or a
  852.      character it is not supposed to skip.
  853.  - Function: skip-syntax-backward SYNTAXES &optional LIMIT
  854.      This function moves point backward across characters whose syntax
  855.      classes are mentioned in SYNTAXES.  It stops when it encounters
  856.      the beginning of the buffer, or position LIMIT (if specified), or a
  857.      character it is not supposed to skip.
  858.  - Function: backward-prefix-chars
  859.      This function moves point backward over any number of characters
  860.      with expression prefix syntax.  This includes both characters in
  861.      the expression prefix syntax class, and characters with the `p'
  862.      flag.
  863.